package cn.topevery.garden.wapi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.topevery.garden.dal.logic.GpsDeviceLogic;
import cn.topevery.garden.dal.logic.GpsObjectRelationLogic;
import cn.topevery.garden.entity.po.GpsDevice;
import cn.topevery.garden.entity.query.GpsDeviceQuery;
import cn.topevery.garden.entity.query.GpsObjectRelationQuery;
import cn.topevery.garden.entity.vo.GpsDeviceVO;
import cn.topevery.garden.entity.vo.GpsObjectRelationVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备表
 *
 * @author code-generate
 * @date 2021-08-30
 */
@Api(tags = "设备表")
@RestController
@RequestMapping("/gpsDevice")
public class GpsDeviceController extends BaseController {


    @Autowired
    GpsDeviceLogic gpsDeviceLogic;

    @Autowired
    private Mapper mapper;

    @Autowired
    private GpsObjectRelationLogic relationLogic;

    /**
     * 获取设备表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取设备表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GpsDeviceVO>> listPage(@RequestBody GpsDeviceQuery query) {
        return JsonResult.ok(gpsDeviceLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 设备表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@Valid @RequestBody GpsDevice device) {
        gpsDeviceLogic.saveOrUpdate(getUser().id.toString(), device);
        return JsonResult.ok("保存成功");
    }

    @ApiOperation("通过id删除")
    @GetMapping(value = "/delById")
    public JsonResult<?> delById(Integer id) {
        GpsObjectRelationQuery query = new GpsObjectRelationQuery();
        query.setGpsId(id);
        DataPager<GpsObjectRelationVO> pager = relationLogic.listPage(query);
        if (pager.total > 0) {
            return JsonResult.error("该设备有绑定人员或者车辆，无法删除！");
        }
        gpsDeviceLogic.delete(getUser().id.toString(), id);
        return JsonResult.ok("删除成功");
    }

    @ApiOperation("通过id获取")
    @GetMapping(value = "/getById")
    public JsonResult<?> getById(Integer id) {
        return JsonResult.ok(gpsDeviceLogic.getEntity(id));
    }


    @ApiOperation("导出设备表信息")
    @GetMapping("/export")
    public void export(GpsDeviceQuery query, HttpServletResponse response) {
        List<GpsDeviceVO> list = gpsDeviceLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "设备表", "sheet1", GpsDeviceVO.class, "设备表", response);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importExcel")
    public JsonResult<?> importExcel(@RequestBody MultipartFile file) {
        try {
            ImportParams importParams = new ImportParams();
            importParams.setHeadRows(1);
            importParams.setNeedVerify(true);
            ExcelImportResult<GpsDeviceVO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), GpsDeviceVO.class, importParams);
            List<GpsDeviceVO> deviceVOList = result.getList();

            if(result.getFailList() != null && !result.getFailList().isEmpty()){
                return JsonResult.error("校验存在不通过的数据行:"+ result.getFailList().stream().map(GpsDeviceVO::getCode).collect(Collectors.joining(",")));
            }
            else {
                List<GpsDevice> devices = deviceVOList.stream()
                        .map(temp -> mapper.map(temp, GpsDevice.class)).collect(Collectors.toList());
                gpsDeviceLogic.insert(getUser().id.toString(), devices);
            }
        }
        catch (Exception ex){
            return JsonResult.error(ex.getMessage());
        }
        return JsonResult.ok("导入成功！");
    }

}
